<template>
  <div class="flex flex-col gap-y-2">
    <div class="flex flex-justify-between card">
      <div class="flex gap-2">
        <el-date-picker
          v-model="weekBegin"
          type="week"
          format="[第] ww [周]"
          placeholder="请选择号源周期"
          style="width: 400px"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
        <el-input v-model="getRegisterSchedulePaginationReq.queryText" class="w-[200px]"> </el-input>
        <el-button @click="query">查询</el-button>
      </div>
      <div></div>
    </div>

    <div class="flex flex-col gap-y-2 card">
      <el-table :data="queryRegisterScheduleRes.data" :style="{ height: tableHeight + 'px' }" stripe border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="clinicLabel" label="号别" width="150" />
        <el-table-column prop="deptName" label="科室" width="150" />
        <el-table-column prop="doctorName" label="医生" width="150" />
        <el-table-column prop="clinicType" label="号类" width="150" />
        <el-table-column label="周期">
          <el-table-column
            v-for="item in weekDayHeaders"
            :key="item.prop"
            :prop="item.prop"
            :label="item.title"
            width="160"
          >
            <template #default="{ row }">
              <div class="flex flex-col">
                <span v-for="value in getDay(item.clinicDate, row.days)" :key="value.workId">
                  {{ value.timeDesc }}-{{ value.registerNum }}
                </span>

                <el-button size="small" @click="onDetail(row.clinicLabel, item.clinicDate)">详情</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="getRegisterSchedulePaginationReq.currentPage"
        v-model:page-size="getRegisterSchedulePaginationReq.pageSize"
        :page-sizes="[20, 30, 40, 50]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryRegisterScheduleRes.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <RegisterScheduleDay ref="registerScheduleDayRef" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { dayjs, ElMessage } from 'element-plus'
import type {
  GetRegisterSchedulePaginationReq,
  RegisterScheduleDayRes,
  RegisterScheduleHeader,
  RegisterScheduleRes,
} from '@/api/register'
import { RegisterBusAPIs } from '@/api/register'
import type { PaginationRes } from '@/api/common'
import RegisterScheduleDay from './components/RegisterScheduleDay.vue'

const registerScheduleDayRef = ref<any>()
const weekBegin = ref<Date>()

const weekDayHeaders = ref<RegisterScheduleHeader[]>([])

/** 表格高度 */
const tableHeight = computed(() => window.innerHeight - 252 - 60)

/** 表分页结果 */
var queryRegisterScheduleRes = ref<PaginationRes<RegisterScheduleRes>>({
  total: 0,
  data: [],
})

/** 表分页请求 */
var getRegisterSchedulePaginationReq = ref<GetRegisterSchedulePaginationReq>({
  queryText: '',
  currentPage: 1,
  pageSize: 20,
})

/** 查找 */
async function query() {
  if (weekBegin.value === undefined) {
    ElMessage.warning('请选择号源周期')
    return
  }

  console.error(weekBegin.value)
  getRegisterSchedulePaginationReq.value.beginDate = dayjs(weekBegin.value).format('YYYY-MM-DD')

  console.error(getRegisterSchedulePaginationReq.value)
  const getRegisterSchedulePaginationRes = await RegisterBusAPIs.getSchedule(getRegisterSchedulePaginationReq.value)

  weekDayHeaders.value = getRegisterSchedulePaginationRes.headers
  queryRegisterScheduleRes.value = getRegisterSchedulePaginationRes.pagination
}

function getDay(clinicDate: Date, days: RegisterScheduleDayRes[]) {
  return days.filter(f => f.clinicDate == clinicDate)
}

/** 页大小改变 */
const handleSizeChange = (val: number) => {
  getRegisterSchedulePaginationReq.value.pageSize = val
}

/** 当前页号 */
const handleCurrentChange = (val: number) => {
  getRegisterSchedulePaginationReq.value.currentPage = val
}

/** 监控 */
watch(
  [
    () => getRegisterSchedulePaginationReq.value.currentPage,
    () => getRegisterSchedulePaginationReq.value.pageSize,
    () => getRegisterSchedulePaginationReq.value.queryText,
  ],
  () => {
    query()
  }
)

/**详情 */
function onDetail(clinicLabel: string, clinicDate: Date) {
  registerScheduleDayRef.value.display(clinicLabel, clinicDate)
}
</script>

<style lang="scss" scoped></style>
